package com.mar.meclient.bt.ui.impl;

import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.mar.meclient.bt.BTInquiryListener;
import com.mar.meclient.bt.IBluetoothConnectionHandler;
import com.mar.meclient.bt.IBluetoothIOHandler;
import com.mar.meclient.bt.impl.BluetoothConnectionHandler;
import com.mar.meclient.util.ui.IPage;
import com.mar.meclient.util.ui.IPageNavigator;

/**
 * TODO: organize UI like eclipse wizard does.
 * @author mar
 *
 */
public class BTInquiryUI implements BTInquiryListener, IPage {
	private static final String CONNECT_CMD = "Connect";
	private static final String STOP_SEARCH_CMD = "Stop";
	private static final String SEARCH_CMD = "Search";
	
	private List uiDevicesList;
	private final IBluetoothConnectionHandler btConnectionHandler;
	private final IPageNavigator uiPageNavigator;
	
	public BTInquiryUI( IPageNavigator navigator ) {
		uiPageNavigator = navigator;
		
		btConnectionHandler = new BluetoothConnectionHandler();
	}
	
	public void createPageContent(Object modelInput) {
		uiDevicesList = new List("Searching devices", List.IMPLICIT);
		// start search cmd.
		uiDevicesList.addCommand(new Command(SEARCH_CMD, 1, Command.ITEM));
		
		uiDevicesList.setCommandListener( new InternalCmdListener() );
	}
	
	public void deviceFound(String device) {
		this.uiDevicesList.append(device, null);
	}

	public void inquiryCompleted() {
		uiDevicesList.addCommand(new Command(CONNECT_CMD, 2, Command.ITEM));
		uiDevicesList.setTitle("Search completed");
	}
	
	private void doConnect() {
		uiDevicesList.setTitle("Connectiong...");
		try {
			IBluetoothIOHandler btIOHandler = btConnectionHandler.connectTo(uiDevicesList.getString(uiDevicesList.getSelectedIndex()));
			uiPageNavigator.navigateNext(this, btIOHandler);
		} catch (BluetoothStateException e) {
			uiDevicesList.setTitle("Search completed");
			uiPageNavigator.showPageProblem(this, e.getMessage() ,e);
		} catch (IOException e) {
			uiDevicesList.setTitle("Search completed");
			uiPageNavigator.showPageProblem(this, e.getMessage() ,e);
		}
	}

	private void doStopSearch() {
		try {
			btConnectionHandler.stopInquiry();
			uiDevicesList.setTitle("Stopped");
			if ( uiDevicesList.size() > 0 ){
				uiDevicesList.addCommand(new Command(CONNECT_CMD, 2, Command.OK));
			}
		} catch (BluetoothStateException e) {
			uiPageNavigator.showPageProblem(this, e.getMessage() ,e);
		}
	}

	private void doSearch(Command c) {
		uiDevicesList.removeCommand(c);
		Command stopCmd = new Command(STOP_SEARCH_CMD, 1, Command.OK);
		uiDevicesList.addCommand(stopCmd);
		
		uiDevicesList.setTitle("Searching...");
		
		try {
			btConnectionHandler.startInquiry(BTInquiryUI.this);
		} catch (BluetoothStateException e) {
			uiPageNavigator.navigateOnCriticalException(this, e);
		} 
	}

	private class InternalCmdListener implements CommandListener{
		public void commandAction(Command c, Displayable d) {
			if (SEARCH_CMD.equals(c.getLabel())) {
				doSearch(c);
			} else if (STOP_SEARCH_CMD.equals(c.getLabel())) {
				doStopSearch();
			} else if (CONNECT_CMD.equals(c.getLabel())) {
				doConnect();
			}
		}
	}

	public Displayable getDesplayable() {
		return uiDevicesList;
	}
}
